package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.domain.vo.GiftWallLikeInfoVO;
import com.juyu.miliao.api.service.AppKeyService;
import com.juyu.miliao.api.service.GiftWallService;
import com.juyu.miliao.api.service.OpenApiService;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.OpenApiStatus;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.util.VerifyUtil;
import com.juyu.miliao.common.util.VersionCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 礼物墙
 *
 * @author xuanqicheng
 * @create 2019-18-17:57
 */
@RestController
@RequestMapping("/giftWall")
@Slf4j
public class GiftWallController {

    @Autowired
    private GiftWallService giftWallService;
    @Resource
    private OpenApiService openApiService;
    @Resource
    private AppKeyService appkeyService;

    @GetMapping("/{page}-{pageSize}")
    public Object getGiftWall(Authentication token, @PathVariable("page") Integer page,
                              @PathVariable("pageSize") Integer pageSize,
                              HttpServletRequest request) {

        if (page <= 0) {
            page = 1;
        }
        Subject subject = (Subject) token.getPrincipal();
        if (pageSize <= 0) {
            pageSize = 20;
        }
        CommonHead com = VerifyUtil.verifyCommonHead(request);
        OpenApiStatus ops = openApiService.getOpenApi(com);
        if (ops != null && ops.getForwardAudit() && ops.getBackAudit()) {
            return appkeyService.getAppKeyByKey("giftWallList").getValue();
        } else {
            String versionNum=com.getVersionNum();
            boolean bl = VersionCheckUtil.compare(versionNum.split("\\."),"1.4.3");
            if(bl){
                return giftWallService.getOldPageInfo(subject.getId(),page,pageSize);
            }else {
                log.info("获取礼物墙，userId=" + subject.getId());
                return giftWallService.getPageInfo(subject.getId(), page, pageSize);
            }


        }
    }

    @PostMapping("/like")
    public Object getLike(Authentication token, Integer sendId) {
        if (sendId == null || sendId == 0) {
            throw new ConsumeRuntimeException("参数不正确");
        }

        Subject subject = (Subject) token.getPrincipal();

        return giftWallService.setLike(subject.getId(), sendId);
    }

    @GetMapping("/like/{sendId}")
    public GiftWallLikeInfoVO getGiftWallLikeInfo(@PathVariable("sendId") Integer sendId) {

        if (sendId == null || sendId == 0) {
            throw new ConsumeRuntimeException("参数不正确");
        }

        return giftWallService.getGiftWallLikeInfo(sendId);

    }


}
